package com.heima.dao;


import com.heima.dao.provider.UserDaoProvider;
import com.heima.pojo.QueryVO;
import com.heima.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {
    //添加方法
    /*
    @Insert：增加方法
    @SelectKey：主键回显
        keyProperty：主键属性名
        keyColumn：主键列名，字段名
        resultType：属性类型
        before：执行查询主键的时机，可选值：true ，false
        statement：执行的sql语句
     */
    @Insert("insert into t_user (id,username,birthday,sex,address)" +
            "values (null,#{username},#{birthday},#{sex},#{address})")
    @SelectKey(keyProperty = "id",keyColumn = "id",resultType = int.class ,
            before = false,statement = "select last_insert_id()")
    int insert(User user);

    //删除方法
    @Delete("delete from t_user where id =#{id}")
    int deleteById(@Param("id")Integer id);

    //修改方法
    @Update("update t_user set username = #{username}, birthday = #{birthday}" +
            ",sex = #{sex}, address = #{address} where id = #{id}")
    int update1(User user);

    @Select("select * from t_user where id = #{id} ")
    //根据id查询方法
    User selectById(@Param("id")Integer id);

    //查询所有用户方法
    @Select("select * from t_user")
    List<User> selectAll();

    //分页查询用户信息的方法
    @Select("select * from t_user limit #{startIndex},#{pageSize}")
    List<User> selectByPage(@Param("startIndex")Integer startIndex,@Param("pageSize")Integer pageSize);

    //分页查询用户信息
    @Select("select * from t_user limit #{param1},#{param2}")
    List<User> selectByPage1(Integer startIndex,Integer pageSize);

    //模糊查询
    //@Result相当于resultMap标签
    @Results({
        @Result(id = true,column = "id", property = "id" ),
        @Result(column = "username", property = "username" ),
        @Result(column = "sex", property = "sex" ),
        @Result(column = "address", property = "address" ),
        @Result(column = "birthday", property = "birthday" ),
    })
    @Select("select * from t_user where username like concat('%',#{username},'%')")
    List<User> selectByLike(String username);

    //创建QueryVO查询
    @SelectProvider(type = UserDaoProvider.class, method = "selectByCondition")
    List<User> selectByCondition(QueryVO queryVO);

    //修改个人信息
    @SelectProvider(type = UserDaoProvider.class,method = "update")
    int update(User user);

    //批量删除用户
    @DeleteProvider(type = UserDaoProvider.class,method = "delete")
    int deleteByIds(Integer[] ids);

    //批量添加方法
    @Insert("<script>" +
            "insert into t_user(username,birthday,sex,address)" +
            "values <foreach collection = 'list' item = 'user' separator = ','> " +
            "(#{user.username},#{user.birthday},#{user.sex},#{user.address})</foreach>"+
            "</script>")
    int insertUserList(@Param("list")List<User> list);



}
